package com.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Course;
import com.entity.CourseCategory;
 
import com.service.CourseCategoryService;
import com.service.CourseService;
import com.util.Page;
import com.util.Pager;

@RequestMapping("/admin/product")
@Controller
public class CourseController {
	
	@Autowired
	private  CourseCategoryService  productCategoryService;

	@Autowired
	private  CourseService  productService;
	/*
	 *课程页表页 
	 * */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(ModelAndView model){
		model.setViewName("product/list");
		model.addObject("productCategoryList", productCategoryService.findList(new HashMap<String,Object>()) );
		return model;
	}
	/*
	 *课程添加页 
	 * */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(ModelAndView model){
		model.setViewName("product/add");
		return model;
	}
	/*
	 *课程修改页 
	 * */
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public ModelAndView edit(ModelAndView model,Long id){
		model.setViewName("product/edit");
		model.addObject("product",productService.findById(id));
		return model;
	}
	/*
	 * 查询课程页表
	 * */
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>list(@RequestParam(value="name",defaultValue="")String name,
			@RequestParam(value="productCategoryId",required=false)String productCategoryId,
			@RequestParam(value="priceMin" ,required=false)Double priceMin,
			@RequestParam(value="priceMax" ,required=false)String priceMax,
			Page  page
			){
		 Map<String,Object> ret=new HashMap<String,Object>();
		 Map<String,Object> queryMap=new HashMap<String,Object>();
		 queryMap.put("name", name);
		 if(!StringUtils.isEmpty(productCategoryId)){
			 queryMap.put("tags", productCategoryId);
		 }
		 if(!StringUtils.isEmpty(priceMin)){
			 queryMap.put("priceMin", priceMin);
		 }
		 if(!StringUtils.isEmpty(priceMax)){
			 queryMap.put("priceMax", priceMax);
		 }
		// queryMap.put("offset", page.getOffset());
		// queryMap.put("pageSize", page.getRows());
		 ret.put("rows", productService.findList(queryMap));
		 ret.put("total",productService.getTotal(queryMap) );
		 return ret;
	}
	
	/*
	 * 添加课程
	 * */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>add(Course  course ){
		 Map<String,Object> ret=new HashMap<String,Object>();
	     if(course ==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写课程信息");
			 return ret;
	     }
	     if(StringUtils.isEmpty(course .getName())){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写课程名称");
			 return ret;
	     }
	     if(course.getImageUrl()==null){
	    	 ret.put("type", "error");
			 ret.put("msg","添填寫课程图片");
			 return ret;
	     }
	     if(course.getPath()==null){
	    	 ret.put("type", "error");
			 ret.put("msg","添填寫课程视频");
			 return ret;
	     }
	     if(course.getPrice()==null){
	    	 ret.put("type", "error");
			 ret.put("msg","添填寫價格");
			 return ret;
	     }
	    
	     CourseCategory productCategory=productCategoryService.findById(course.getcourseCategoryId());
	     course.setTags(productCategory.getTags()+","+productCategory.getId());
	     course.setCreateTime(new Date());
	     if(productService.add(course)<=0){
	    	 ret.put("type", "error");
			 ret.put("msg","添加失败请联系管理员");
			 return ret;
	     }
	     ret.put("type", "success");
		 ret.put("msg","添加成功");
		 return ret;
	}
	/*
	 * 编辑课程
	 * */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>edit(Course  product ){
		 Map<String,Object> ret=new HashMap<String,Object>();
		 if(product ==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写课程信息");
			 return ret;
	     }
	     if(StringUtils.isEmpty(product .getName())){
	    	 ret.put("type", "error");
			 ret.put("msg","请填写课程名称");
			 return ret;
	     }
	     if(product.getcourseCategoryId()==null){
	    	 ret.put("type", "error");
			 ret.put("msg","添填寫课程分類");
			 return ret;
	     }
	     if(product.getPrice()==null){
	    	 ret.put("type", "error");
			 ret.put("msg","添填寫课程價格");
			 return ret;
	     }
	     if(StringUtils.isEmpty(product.getImageUrl())){
	    	 ret.put("type", "error");
			 ret.put("msg","请上傳课程主要圖");
			 return ret;
	     }
	     CourseCategory productCategory=productCategoryService.findById(product.getcourseCategoryId());
	     product.setTags(productCategory.getTags()+","+productCategory.getId());
	   
	     if(productService.edit(product)<=0){
	    	 ret.put("type", "error");
			 ret.put("msg","编辑失败请联系管理员");
			 return ret;
	     }
	     ret.put("type", "success");
		 ret.put("msg","编辑成功");
		 return ret;
	}
	/*
	 * 删除课程
	 * */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object>delete(Long id){
		 Map<String,Object> ret=new HashMap<String,Object>();
	     if(id==null){
	    	 ret.put("type", "error");
			 ret.put("msg","请选择要删除的信息");
			 return ret;
	     }
	     try{
	    	 if(productService.delete(id)<=0){
		    	 ret.put("type", "error");
				 ret.put("msg","编辑失败请联系管理员");
				 return ret;
		     }
	     }catch(Exception e){
	    	 ret.put("type", "error");
			 ret.put("msg","该分类存在订单信息，不许删除！");
			 return ret;
	     }
	     
	     ret.put("type", "success");
		 ret.put("msg","删除成功");
		 return ret;
	}
	
	 
	/**
	 * 返回树形分类
	 * @return
	 */
	@RequestMapping(value="/tree_list",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> treeList(){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		return getTreeCategory(productCategoryService.findList(queryMap));
	}
	/**
	 * 根据列表生成三级树形关系分类
	 * @param productCategorieList
	 * @return
	 */
	private List<Map<String, Object>> getTreeCategory(List<CourseCategory> productCategorieList){
		List<Map<String, Object>> ret = new ArrayList<Map<String,Object>>();
		//所有的父分类整理
		for(CourseCategory productCategory : productCategorieList){
			if(productCategory.getParentId() == null){
				Map<String, Object> top = new HashMap<String, Object>();
				top.put("id", productCategory.getId());
				top.put("text", productCategory.getName());
				top.put("children", new ArrayList<Map<String,Object>>());
				ret.add(top);
			}
		}
		//所有的二级类整理
		for(CourseCategory productCategory : productCategorieList){
			if(productCategory.getParentId() != null){
				for(Map<String, Object> map : ret){
					if(productCategory.getParentId().longValue() == Long.valueOf(map.get("id")+"")){
						List children = (List)map.get("children");
						Map<String, Object> child = new HashMap<String, Object>();
						child.put("id", productCategory.getId());
						child.put("text", productCategory.getName());
						child.put("children",new ArrayList<Map<String,Object>>());
						children.add(child);
					}
				}
			}
		}
		
			//所有的三级分类整理
			for(CourseCategory productCategory : productCategorieList){
				if(productCategory.getParentId() != null){
					for(Map<String, Object> map : ret){
						//获取二级分类
						List<Map<String,Object>> children = (List<Map<String,Object>>)map.get("children");
							for(Map<String,Object>child:children){
								if(productCategory.getParentId().longValue() == Long.valueOf(child.get("id")+"")){
									List grandsons=(List)child.get("children");
									Map<String, Object> grandson = new HashMap<String, Object>();
									grandson.put("id", productCategory.getId());
									grandson.put("text", productCategory.getName());
									grandson.put("children",new ArrayList<Map<String,Object>>());
									grandsons.add(grandson);
								}
							}
							
					}
				}
		}
		return ret;
	}
}
 





















